/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatclient2;

import java.net.*;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author theInterloper
 */
public class ChatClient2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String sm, m = null;
        Socket client;
        ObjectOutputStream output = null;
        ObjectInputStream input = null;
        
        try{
            client = new Socket (InetAddress.getLocalHost(), 5001);
            output = new ObjectOutputStream (client.getOutputStream());
            output.flush();
            
            input = new ObjectInputStream(client.getInputStream());
            Screen.message("got I/O streams\n");
            
            try{
                m = (String) input.readObject();
                Screen.message("Message sent to client from server \n" + m);
            }catch (ClassNotFoundException cnf){
                Screen.message(cnf.getMessage());
            }
            do{
                Screen.message("Enter message to write to server (1 to end)");
                
                Scanner scan = new Scanner (System.in);
                sm = scan.nextLine();
                
                try{
                    System.out.println("track: " + sm);
                    output.writeObject(sm);
                    output.flush();
                }catch (IOException ex){
                    Screen.message(ex.getMessage());
                }
                try {
                    m = (String) input.readObject();
                    Screen.message(m);
                } catch (ClassNotFoundException cnf){
                Screen.message(cnf.getMessage());
                }
                
            }while (!(sm.equals("1")));
            output.close();
            input.close();
            client.close();
        }catch(EOFException eof){
            Screen.message("Client termination exception");
        }catch (IOException e){
            Screen.message(e.getMessage());
        }
        
    }
}
